﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sexybeauty.Core.Inf.Entities.Table;
using Sexybeauty.Core.Inf.Entities;
using Sexybeauty.Framework.Common;
using Sexybeauty.Framework.Cache;
using Sexybeauty.Core.Inf.LogicEntity.Cache;
using System.Linq.Expressions;

namespace Sexybeauty.Core.Inf.LogicEntity
{
    public class MemberEntity : IAccessible<Member,MemberEntity>
    {
        public MemberEntity() { }
        public MemberEntity(Member member) 
        {
            if (member != null)
            {
                Covariant(member);
            }
        }
        #region Fileds
        /// <summary>
        /// 邮箱
        /// </summary>
        public string UserName { get; set; }
        public string NickName { get; set; }
        public string Password { get; set; }
        public Gender Gender { get; set; }
        public DateTime Birthday { get; set; }
        public SocialRole Role { get; set; }
        public AreaCode Area { get; set; }
        public int Location { get; set; }
        public string QQ { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public DateTime RcdDate { get; set; }
        public bool IsRefuse { get; set; } 
        public bool IsActivate { get; set; }
        public string Token { get; set; }

        public MemberDetailEntity MemberDetail { get; set; }
        public MemberAccountEntity MemberAccount { get; set; }
        #endregion

        #region Override
        public override MemberEntity Element(System.Linq.Expressions.Expression<Func<Member, bool>> where)
        {
            string expressionParam = where.Parameters[0].Name;
            if (where.Body is BinaryExpression)
            {
                var be = where.Body as BinaryExpression;
                if (be.Left.NodeType == ExpressionType.MemberAccess &&
                    (be.Right.NodeType == ExpressionType.Constant || be.Right.NodeType == ExpressionType.MemberAccess))
                {
                    var filed = be.Left.ToString().Replace(expressionParam + ".", "");
                    if ("UserName".Equals(filed, StringComparison.CurrentCultureIgnoreCase))
                    {
                        String val = String.Empty;
                        if (be.Right.NodeType == ExpressionType.MemberAccess)
                        {
                            val = Expression.Lambda(be.Right).Compile().DynamicInvoke().ToString();
                        }
                        else
                        {
                            val = (be.Right as ConstantExpression).Value.ToString();
                        } 
                        return new MemberCache().GetItem(val);
                    }
                }
            } 
            return base.Element(where);
        }
        public override MemberEntity PostModify()
        {
            var entity = base.PostModify();
            new MemberCache().RefreshItem(entity.UserName);
            return entity;
        }
        public override MemberEntity PostDelete()
        {
            var entity = base.PostDelete();
            new MemberCache().RefreshItem(entity.UserName);
            return entity;
        }
        #endregion

        #region Dependence
        public void LoadMemberDetail() 
        {
            if (!String.IsNullOrEmpty(UserName))
            {
                MemberDetail = new MemberDetailCache().GetItem(UserName);
            }
        }
        public void LoadMemberAccount()
        {
            if (!String.IsNullOrEmpty(UserName))
            {
                MemberAccount = new MemberAccountEntity().Element(c => c.UserName == UserName);
            }
        }
        #endregion

        protected override void Covariant(Member dbModel)
        {
            this.UserName = dbModel.UserName;
            this.NickName = dbModel.NickName;
            this.Password = dbModel.Password;
            this.Gender = dbModel.Gender ? LogicEntity.Gender.Female : LogicEntity.Gender.Male;
            this.Birthday = dbModel.Birthday;
            this.Role = (SocialRole)dbModel.Role;
            this.Area = (AreaCode)dbModel.Area;
            this.Location = dbModel.Location;
            this.QQ = dbModel.QQ;
            this.Phone = dbModel.Phone;
            this.Email = dbModel.Email;
            this.RcdDate = dbModel.RcdDate;
            this.IsRefuse = dbModel.IsRefuse;
            this.Token = dbModel.Token;
            this.IsActivate = dbModel.IsActivate;
        }
        protected override Member Inverter(MemberEntity model)
        {
            return new Member()
            {
                Area = (int)model.Area,
                Birthday = model.Birthday,
                Email = model.Email,
                Gender = model.Gender == LogicEntity.Gender.Male ? false : true,
                IsRefuse = model.IsRefuse,
                Location = model.Location,
                NickName = model.NickName,
                Password = model.Password,
                Phone = model.Phone,
                QQ = model.QQ,
                RcdDate = model.RcdDate,
                Role = (int)model.Role,
                UserName = model.UserName,
                Token = model.Token,
                IsActivate=model.IsActivate
            };
        }
    }

}
